﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CodeLibrary.Consts
{
    public static class FileTypes
    {
        public static class Images
        {
            public const string PNGExtention = ".png";
            public const string JPGExtention = ".jpg";
            public const string GIFExtention = ".gif";
            public const string BMPExtention = ".bmp";
            public const string JPEGExtention = ".jpeg";
            public const string TIFFExtention = ".tiff";

            public static bool IsImageFile(string path)
            {
                if (String.IsNullOrEmpty(path) || !File.Exists(path))
                {
                    return false;
                }
                string extension = Path.GetExtension(path);
                if (String.IsNullOrEmpty(extension))
                {
                    return false;
                }
                string formatedExtention = extension.ToLower();
                return formatedExtention == PNGExtention
                       || formatedExtention == JPGExtention
                       || formatedExtention == GIFExtention
                       || formatedExtention == BMPExtention
                       || formatedExtention == JPEGExtention
                       || formatedExtention == TIFFExtention;
            }

            public static PropertyItem GetImageProperty(string path, int propertyId)
            {
                if (!IsImageFile(path))
                {
                    return null;
                }
                FileStream fs = File.OpenRead(path);
                Image img = Image.FromStream(fs, false, false);
                bool propertyFound = false;
                for (int i = 0; i < img.PropertyIdList.Length; i++)
                {
                    if (img.PropertyIdList[i] == propertyId)
                    {
                        propertyFound = true;
                        break;
                    }
                }
                if (!propertyFound)
                {
                    return null;
                }
                PropertyItem p = img.GetPropertyItem(propertyId);
                fs.Close();
                img.Dispose();
                return p;
            }

            public const int THUMBNAIL_DATA = 0x501B;
        }
    }
}
